﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;
namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PZZLManagerController : AdminBaseController
    {
        [AuthorityActionFilter("m_DM_PZZL", Order = 1)]
        [LogPageActionFilter("凭证种类管理", Order = 2)]
        public ActionResult Index()
        {
            // string s = Request.Params["index"];
            string strwhere = " PZZL_DM is not null ";
            List<Model.DM_PZZL> DM_PZZLList = new List<Model.DM_PZZL>();
            DM_PZZLList = GetDM_PZZLListList(strwhere, 1);
            return View(DM_PZZLList);
        }



        public ActionResult DM_PZZLAuditListPartial()
        {
            BLL.DM_PZZL codeNameBll = new BLL.DM_PZZL();
            List<Model.DM_PZZL> codeNameList = new List<Model.DM_PZZL>();
            codeNameList = codeNameBll.GetModelList("");
            ViewData["blcode"] = true;
            return PartialView("DM_PZZLAuditListPartial", codeNameList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.DM_PZZL> GetDM_PZZLListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.DM_PZZL DM_PZZLBLL = new BLL.DM_PZZL();
            string fiedls = "PZZL_DM";
            int total;
            //DataSet ds = DM_PZZLBLL.GetDM_PZZLListList(strWhere, pageIndex);
            DataSet ds = commonBll.GetListByPage("ltais.DM_PZZL", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "PZZL_DM", out total);
            List<Model.DM_PZZL> DM_PZZLList = DM_PZZLBLL.DataTableToList(ds.Tables[0]);
            List<Model.DM_PZZL> DM_PZZLAllList = new List<Model.DM_PZZL>();
            total = DM_PZZLBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                DM_PZZLAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (DM_PZZLList.Count > i)
                {
                    DM_PZZLAllList[((pageIndex - 1) * pageRowSize) + i] = DM_PZZLList[i];
                }
            }

            return DM_PZZLAllList;
        }
        public string DelPZZL(string PZZL_DM)
        {
            string str = string.Empty;
            if (null != PZZL_DM)
            {
                PZZL_DM = Fetch.estimateString(PZZL_DM, CheckGetEnum.Safety);
                BLL.DM_PZZL codeNameBll = new BLL.DM_PZZL();
                try
                {
                    codeNameBll.Delete(PZZL_DM);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }
        public ActionResult DM_PZZLAuditEdit(string PZZL_DM)
        {
            ViewData["PZZL_DM"] = PZZL_DM;
            Model.DM_PZZL codeNameObj = new Model.DM_PZZL();
            ViewData["blcodeNo"] = true;
            BLL.TaxClass taxClassBLL = new BLL.TaxClass();
            ViewData["TaxClassList"] = taxClassBLL.GetModelList("");
            if (null != PZZL_DM)
            {
                PZZL_DM = Fetch.estimateString(PZZL_DM, CheckGetEnum.Safety);
                BLL.DM_PZZL codeNameBll = new BLL.DM_PZZL();
                codeNameObj = codeNameBll.GetModel(PZZL_DM);
                if (null == codeNameObj)
                {
                    codeNameObj = new Model.DM_PZZL();
                }
                else
                {
                    ViewData["blcodeNo"] = false;
                }
            }
            //return View(codeNameObj);
            return View("DM_PZZLAuditEdit", codeNameObj);
        }

        public ActionResult Save()
        {

            var PZZL_DM = Request.Params["tbPZZL_DM"];
            var PZZL_MC = Request.Params["tbPZZL_MC"];
            var YSPZ_DM = Request.Params["tbYSPZ_DM"];
            var SWWS_DM = Request.Params["tbSWWS_DM"];
            var YZ_XYBZ = Request.Params["tbYZ_XYBZ"];
            var SB_XYBZ = Request.Params["tbSB_XYBZ"];
            var PZ_XYBZ = Request.Params["tbPZ_XYBZ"];
            var TS_XYBZ = Request.Params["tbTS_XYBZ"];
            var JM_XYBZ = Request.Params["tbJM_XYBZ"];
            var JKS_XYBZ = Request.Params["tbJKS_XYBZ"];
            var WSZ_XYBZ = Request.Params["tbWSZ_XYBZ"];
            var PZDL_DM = Request.Params["tbPZDL_DM"];
            var PZZL_DL_DM = Request.Params["tbPZZL_DL_DM"];
            var PZZL_DM_SX = Request.Params["tbPZZL_DM_SX"];
            var GS_PZZL_MC = Request.Params["tbGS_PZZL_MC"];
            var YXBZ = Request.Params["tbYXBZ"];
            BLL.DM_PZZL codeNameBll = new BLL.DM_PZZL();
            var isAdd = Convert.ToBoolean(Request.Params["isAdd"]);
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(PZZL_DM))
                {
                    ViewData["jsAlert"] = "编号已存在";
                    return DM_PZZLAuditEdit(Fetch.Post("PZZL_DM"));
                }
            }

            Model.DM_PZZL codeNameObj = new Model.DM_PZZL();
            codeNameObj.PZZL_DM = PZZL_DM;
            codeNameObj.PZZL_MC = PZZL_MC;
            codeNameObj.YSPZ_DM = YSPZ_DM;
            codeNameObj.SWWS_DM = SWWS_DM;
            codeNameObj.YZ_XYBZ = (YZ_XYBZ == "C") ? "Y" : "N";
            codeNameObj.SB_XYBZ = (SB_XYBZ == "C") ? "Y" : "N";
            codeNameObj.PZ_XYBZ = (PZ_XYBZ == "C") ? "Y" : "N";
            codeNameObj.TS_XYBZ = (TS_XYBZ == "C") ? "Y" : "N";
            codeNameObj.JM_XYBZ = (JM_XYBZ == "C") ? "Y" : "N";
            codeNameObj.JKS_XYBZ = (JKS_XYBZ == "C") ? "Y" : "N";
            codeNameObj.WSZ_XYBZ = (WSZ_XYBZ == "C") ? "Y" : "N";
            codeNameObj.PZDL_DM = PZDL_DM;
            codeNameObj.PZZL_DL_DM = PZZL_DL_DM;
            codeNameObj.PZZL_DM_SX = PZZL_DM_SX;
            codeNameObj.GS_PZZL_MC = GS_PZZL_MC;
            codeNameObj.YXBZ = (YXBZ == "C") ? "Y" : "N";
            try
            {
                //Model.DM_PZZL model = new Model.DM_PZZL();
                //model.FFileNo = Fetch.Post("FFileNo");
                //model.FFileTransDate = DateTime.Now;
                //model.FNote = Fetch.Post("FNote");
                //model.FDM_PZZLID = Fetch.Post("hidFDM_PZZLID");
                //BLL.DM_PZZL allowanceFileBLL = new BLL.DM_PZZL();
                //allowanceFileBLL.Update(model);
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj);
                }
                else
                {
                    codeNameBll.Update(codeNameObj);
                }
                ViewData["jsAlert"] = "操作成功";
            }
            catch (Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }

            return DM_PZZLAuditEdit(Fetch.Post("PZZL_DM"));
        }

        public DataTable GetLookData(DataTable dt)
        {
            dt.Columns["FAmount"].DataType = typeof(decimal);
            decimal sumse = dt.AsEnumerable().Sum(p => p.Field<decimal>("FAmount"));
            DataRow dr = dt.NewRow();
            dr["NSRMC"] = "合计";
            dr["FAmount"] = sumse;
            dt.Rows.Add(dr);
            return dt;

        }
    }

}
